package com.hippo.core.ui.compose.login

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hippo.core.R
import com.hippo.core.config.AppStoreKey
import com.hippo.core.core.ext.orZero
import com.hippo.core.core.manager.AppStore
import com.hippo.core.model.WaterEnvi
import com.hippo.core.state.DeviceValue
import com.hippo.core.tuya.ThingControl.celsiusToFahrenheit
import com.hippo.core.ui.components.TitleBar
import com.hippo.core.ui.theme.ColorBackgroundPrimary
import com.hippo.core.ui.theme.HippoTheme

@Composable
fun WaterEnviScreen(deviceValue: DeviceValue, onBack:()->Unit) {

//    var uiState by remember { mutableStateOf(WaterEnvi()) }


    Column(
        modifier = Modifier.fillMaxSize()
    ) {

        TitleBar(
            title = stringResource(id = R.string.water_title),
            onBack = onBack
        )
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(color = ColorBackgroundPrimary)
                .padding(16.dp)
        ) {


            Row {
                Column(
                    modifier = Modifier
                        .weight(1f)
                        .background(
                            color = Color.White,
                            shape = MaterialTheme.shapes.medium
                        )
                        .padding(16.dp)
                ){
                    Text(
                        text = stringResource(id = R.string.water_1),
                        style = TextStyle(
                            color = Color.Black,
                            fontWeight = FontWeight.Bold,
                            fontSize = 25.sp
                        )
                    )
                    Text(
                        text =  "${deviceValue.water_quality_ph.orZero()}",
                        style = TextStyle(
                            color = Color.Gray,
                            fontSize = 25.sp
                        )
                    )

                }

                Spacer(modifier = Modifier.width(16.dp))

                Column(
                    modifier = Modifier
                        .weight(1f)
                        .background(
                            color = Color.White,
                            shape = MaterialTheme.shapes.medium
                        )
                        .padding(16.dp)
                ){
                    Text(
                        stringResource(id = R.string.water_2),
                        style = TextStyle(
                            color = Color.Black,
                            fontWeight = FontWeight.Bold,
                            fontSize = 25.sp
                        )
                    )
                    Text(
                        text = "${deviceValue.water_quality_oxygen.orZero()} mV",
                        style = TextStyle(
                            color = Color.Gray,
                            fontSize = 25.sp
                        )
                    )

                }

            }

            Spacer(modifier = Modifier.height(16.dp))


            Row {
                Column(
                    modifier = Modifier
                        .weight(1f)
                        .background(
                            color = Color.White,
                            shape = MaterialTheme.shapes.medium
                        )
                        .padding(16.dp)
                ){
                    Text(
                        text = stringResource(id = R.string.water_3),
                        style = TextStyle(
                            color = Color.Black,
                            fontWeight = FontWeight.Bold,
                            fontSize = 25.sp
                        )
                    )
                    Text(
                        text = "${deviceValue.water_quality_ec.orZero()} mS/cm",
                        style = TextStyle(
                            color = Color.Gray,
                            fontSize = 25.sp
                        )
                    )

                }

                Spacer(modifier = Modifier.width(16.dp))

                Column(
                    modifier = Modifier
                        .weight(1f)
                        .background(
                            color = Color.White,
                            shape = MaterialTheme.shapes.medium
                        )
                        .padding(16.dp)
                ){
                    Text(
                        stringResource(id = R.string.water_4),
                        style = TextStyle(
                            color = Color.Black,
                            fontWeight = FontWeight.Bold,
                            fontSize = 25.sp
                        )
                    )
                    Text(
                        text = "${deviceValue.water_quality_salt.orZero()} mV",
                        style = TextStyle(
                            color = Color.Gray,
                            fontSize = 25.sp
                        )
                    )

                }

            }

            Spacer(modifier = Modifier.height(16.dp))

            Row {
                Column(
                    modifier = Modifier
                        .weight(1f)
                        .background(
                            color = Color.White,
                            shape = MaterialTheme.shapes.medium
                        )
                        .padding(16.dp)
                ){
                    Text(
                        text = stringResource(id = R.string.water_5),
                        style = TextStyle(
                            color = Color.Black,
                            fontWeight = FontWeight.Bold,
                            fontSize = 25.sp
                        )
                    )

                    val temp = AppStore.readString(AppStoreKey.TempUnit)

                    Text(
                        text =   "${deviceValue.water_quality_temperature?.celsiusToFahrenheit()} °F",
                        style = TextStyle(
                            color = Color.Gray,
                            fontSize = 25.sp
                        )
                    )

                }

                Spacer(modifier = Modifier.width(16.dp))

                Column(
                    modifier = Modifier
                        .weight(1f)
                        .padding(16.dp)
                ){


                }

            }

            Spacer(modifier = Modifier.height(36.dp))

            Text(
                text = stringResource(id = R.string.water_6),
                style = TextStyle(
                    color = Color.Gray,
                    fontSize = 16.sp
                )
            )
            Spacer(modifier = Modifier.height(10.dp))
            Text(
                text = stringResource(id = R.string.water_7),
                style = TextStyle(
                    color = Color.Gray,
                    fontSize = 16.sp
                )
            )

        }

    }

}


@Preview
@Composable
private fun WaterEnviScreenPreview() {
    HippoTheme {
        WaterEnviScreen(
            deviceValue = DeviceValue(
                water_quality_ph = 7,
                water_quality_salt = 0,
                water_quality_oxygen = 0,
                water_quality_temperature = 0,
                water_quality_ec = 0
            ),
            onBack = {})
    }

}
